//
//  ViewController.m
//  SASA
//
//  Created by 解晓东 on 15/11/15.
//  Copyright (c) 2015年 sasa. All rights reserved.
//

#import "ViewController.h"
#import "BDRecognizerViewController.h"
#import "BDRecognizerViewDelegate.h"
#import "BDVRFileRecognizer.h"
#import "BDVRRawDataRecognizer.h"
#import "BDRecognizerViewParamsObject.h"
#import "BDVoiceRecognitionClient.h"

#define API_KEY @"iWY5Ui1y9D5S3IbqNn3MYZDE" // 请修改为您在百度开发者平台申请的API_KEY
#define SECRET_KEY @"9e9679612bbd7c83aaf34093e1559662" // 请修改您在百度开发者平台申请的

@interface ViewController () <BDRecognizerViewDelegate, MVoiceRecognitionClientDelegate>
@property (nonatomic, retain) BDRecognizerViewController *recognizerViewController;
@property (weak, nonatomic) IBOutlet UILabel *SAShowLabel;

@end

@implementation ViewController

- (IBAction)start:(id)sender {
    // 创建识别控件
    BDRecognizerViewController *tmpRecognizerViewController = [[BDRecognizerViewController alloc] initWithOrigin:CGPointMake(9, 128) withTheme:[BDTheme lightBlueTheme]];
    
    // 设置全屏
    tmpRecognizerViewController.enableFullScreenMode = YES;
    
    tmpRecognizerViewController.delegate = self;
    self.recognizerViewController = tmpRecognizerViewController;
    
    // 设置识别参数
    BDRecognizerViewParamsObject *paramsObject = [[BDRecognizerViewParamsObject alloc] init];
    
    // 开发者信息，必须修改API_KEY和SECRET_KEY为在百度开发者平台申请得到的值，否则示例不能工作
    paramsObject.apiKey = API_KEY;
    paramsObject.secretKey = SECRET_KEY;
    
    // 设置是否需要语义理解，只在搜索模式有效
    paramsObject.isNeedNLU = NO;
    
    // 设置识别语言
    paramsObject.language = EVoiceRecognitionLanguageChinese;
    
    // 设置识别模式，分为搜索和输入
    paramsObject.recogPropList = @[[NSNumber numberWithInt: EVoiceRecognitionPropertySearch]];
    
    // 设置城市ID，当识别属性包含EVoiceRecognitionPropertyMap时有效
    paramsObject.cityID = 1;
    
    // 开启联系人识别
    //    paramsObject.enableContacts = YES;
    
    // 设置显示效果，是否开启连续上屏
    paramsObject.resultShowMode = BDRecognizerResultShowModeContinuousShow;
    
    // 设置提示音开关，是否打开，默认打开
    paramsObject.recordPlayTones = EBDRecognizerPlayTonesRecordPlay;
    
    paramsObject.isShowTipAfterSilence = NO;
    paramsObject.isShowHelpButtonWhenSilence = NO;
    paramsObject.tipsTitle = @"可以使用如下指令记账";
    paramsObject.tipsList = [NSArray arrayWithObjects:@"我要记账", @"买苹果花了十块钱", @"买牛奶五块钱", @"第四行滚动后可见", @"第五行是最后一行", nil];
    
    [_recognizerViewController startWithParams:paramsObject];
}

#pragma mark - BDRecognizerViewDelegate

- (void)onEndWithViews:(BDRecognizerViewController *)aBDRecognizerView withResults:(NSArray *)aResults
{
    NSLog(@"onEndWithViews");
    _SAShowLabel.text = nil;
    
    if ([[BDVoiceRecognitionClient sharedInstance] getRecognitionProperty] != EVoiceRecognitionPropertyInput)
    {
        // 搜索模式下的结果为数组，示例为
        // ["公园", "公元"]
        NSMutableArray *audioResultData = (NSMutableArray *)aResults;
        NSMutableString *tmpString = [[NSMutableString alloc] initWithString:@""];
        
        for (int i=0; i < [audioResultData count]; i++)
        {
            [tmpString appendFormat:@"%@\r\n",[audioResultData objectAtIndex:i]];
        }
        
        _SAShowLabel.text = [_SAShowLabel.text stringByAppendingString:tmpString];
        _SAShowLabel.text = [_SAShowLabel.text stringByAppendingString:@"\n"];
    
    }
    else
    {
        // 输入模式下的结果为带置信度的结果，示例如下：
        //  [
        //      [
        //         {
        //             "百度" = "0.6055192947387695";
        //         },
        //         {
        //             "摆渡" = "0.3625582158565521";
        //         },
        //      ]
        //      [
        //         {
        //             "一下" = "0.7665404081344604";
        //         }
        //      ],
        //   ]
        NSMutableString *tmpString = [[NSMutableString alloc] initWithString:@""];
        for (NSArray *result in aResults)
        {
            NSDictionary *dic = [result objectAtIndex:0];
            NSString *candidateWord = [[dic allKeys] objectAtIndex:0];
            [tmpString appendString:candidateWord];
        }
        
        _SAShowLabel.text = [_SAShowLabel.text stringByAppendingString:tmpString];
        _SAShowLabel.text = [_SAShowLabel.text stringByAppendingString:@"\n"];
    }
}

- (void)VoiceRecognitionClientWorkStatus:(int) aStatus obj:(id)aObj
{
    NSLog(@"VoiceRecognitionClientWorkStatus");
    switch (aStatus)
    {
        case EVoiceRecognitionClientWorkStatusFinish:
        {
            if ([[BDVoiceRecognitionClient sharedInstance] getRecognitionProperty] != EVoiceRecognitionPropertyInput)
            {
                NSMutableArray *audioResultData = (NSMutableArray *)aObj;
                NSMutableString *tmpString = [[NSMutableString alloc] initWithString:@""];
                
                for (int i=0; i<[audioResultData count]; i++)
                {
                    [tmpString appendFormat:@"%@\r\n",[audioResultData objectAtIndex:i]];
                }
                self.SAShowLabel.text = nil;
                self.SAShowLabel.text = tmpString;
                //[tmpString release];
            }
            else
            {
                self.SAShowLabel.text = nil;
                NSMutableString *tmpString = [[NSMutableString alloc] initWithString:@""];
                for (NSArray *result in aObj)
                {
                    NSDictionary *dic = [result objectAtIndex:0];
                    NSString *candidateWord = [[dic allKeys] objectAtIndex:0];
                    [tmpString appendString:candidateWord];
                }
                self.SAShowLabel.text = tmpString;
            }
            
            break;
        }
        case EVoiceRecognitionClientWorkStatusFlushData:
        {
            NSMutableString *tmpString = [[NSMutableString alloc] initWithString:@""];
            
            [tmpString appendFormat:@"%@",[aObj objectAtIndex:0]];
            self.SAShowLabel.text = nil;
            self.SAShowLabel.text = tmpString;
            //[tmpString release];
            
            break;
        }
        case EVoiceRecognitionClientWorkStatusReceiveData:
        {
            if ([[BDVoiceRecognitionClient sharedInstance] getRecognitionProperty] == EVoiceRecognitionPropertyInput)
            {
                self.SAShowLabel.text = nil;
                NSMutableString *tmpString = [[NSMutableString alloc] initWithString:@""];
                for (NSArray *result in aObj)
                {
                    NSDictionary *dic = [result objectAtIndex:0];
                    NSString *candidateWord = [[dic allKeys] objectAtIndex:0];
                    [tmpString appendString:candidateWord];
                }
                self.SAShowLabel.text = tmpString;
            }
            
            break;
        }
        case EVoiceRecognitionClientWorkStatusEnd:
        {
            break;
        }
        default:
        {
            break;
        }
    }
}

- (void)VoiceRecognitionClientErrorStatus:(int) aStatus subStatus:(int)aSubStatus
{
    
}

- (void)VoiceRecognitionClientNetWorkStatus:(int) aStatus
{
    
}

- (void)clean
{
    _SAShowLabel.text = nil;// 清除result和_SAShowLabel，避免结果与log不对应
}

- (void)cleanSAShowLabelAfter:(int)length
{
    _SAShowLabel.text = [_SAShowLabel.text substringToIndex:length];
}

- (void)logOutToContinusManualResut:(NSString *)aResult
{
    _SAShowLabel.text = aResult;
}

- (void)logOutToManualResut:(NSString *)aResult
{
    NSString *tmpString = _SAShowLabel.text;
    
    if (tmpString == nil || [tmpString isEqualToString:@""])
    {
        _SAShowLabel.text = aResult;
    }
    else
    {
        _SAShowLabel.text = [_SAShowLabel.text stringByAppendingString:aResult];
    }
}
@end
